package com.longxi.tianjintianbao.widget;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.listener.SearchListener;
import com.longxi.tianjintianbao.model.Search;
import com.longxi.tianjintianbao.utils.MyUtils;

public class SearchView extends RelativeLayout implements View.OnFocusChangeListener, View.OnClickListener {
    Context mContext;
    Search mSearch;
    SearchListener mListener;

    LinearLayout mLlSearchBtn;
    LinearLayout mLlEditText;
    TextView mTvSearchName;
    EditText mEtKeyword;
    TextView mTvCancle;
    Button mBtnSearch;

    InputMethodManager imm;
    boolean mIsShowEdit = true;

    public SearchView(Context context) {
        super(context);
    }

    public SearchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }

    public void setDate(Search search, SearchListener listener){
        mSearch = search;
        mListener = listener;
        control();
    }

    public void setDate(Search search, SearchListener listener, boolean isShowEdit){
        mSearch = search;
        mListener = listener;
        mIsShowEdit = isShowEdit;
        control();
    }

    private void control()
    {
        initialise(); // 实例化内部view
//        if(!"".equals(mSearch.getActivity()) || mSearch.getActivity() != null){
//            mContext.startActivity(new Intent(mContext, mSearch.getActivity()));
//            return;
//        }
        mTvSearchName.setText(mSearch.getName());
        mEtKeyword.setHint(mSearch.getName());
        imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);

        if(mListener == null){
            return;
        }
        showHideEdit();
        setViewListener();
    }

    private void initialise() {
        LayoutInflater.from(mContext).inflate(R.layout.view_search, this);
        mLlSearchBtn = (LinearLayout) findViewById(R.id.llSearchBtn);
        mTvSearchName = (TextView) findViewById(R.id.tvSearchName);
        mLlEditText = (LinearLayout) findViewById(R.id.llEditText);
        mEtKeyword = (EditText) findViewById(R.id.etKeyword);
        mTvCancle = (TextView) findViewById(R.id.tvCancle);
        mBtnSearch = (Button) findViewById(R.id.btnSearch);
    }

    private void setViewListener() {

        mLlSearchBtn.setOnClickListener(this);
        mTvCancle.setOnClickListener(this);

        mEtKeyword.setOnEditorActionListener(new TextView.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                //回车时返回true拦截事件，不让换行
                if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                    if (TextUtils.isEmpty(mEtKeyword.getText().toString().trim())) {
                        MyUtils.showToast("请输入搜索内容");
                    } else {//不为空时才添加标签
                        mListener.onSearch(mEtKeyword.getText().toString().trim());
                    }
                    return true;
                }
                return false;
            }
        });

        mEtKeyword.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(s.length() > 0){
//                    imageViewLeft.setImageResource(pressImage);
//                    imageViewRight.setVisibility(View.VISIBLE);
                }else{
//                    imageViewLeft.setImageResource(nomalImage);
//                    imageViewRight.setVisibility(View.GONE);
                }
            }
        });

        mBtnSearch.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(mEtKeyword.getText().toString().trim())) {
                    MyUtils.showToast("请输入搜索内容");
                } else {//不为空时才添加标签
                    mListener.onSearch(mEtKeyword.getText().toString().trim());
                }
            }
        });
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        switch (v.getId()) {
            case R.id.etKeyword:
                if(hasFocus){
                    //imageViewLeft.setImageResource(pressImage);
                }else{
                    //imageViewLeft.setImageResource(nomalImage);
                }
                break;

            default:
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvCancle:
                showHideEdit();
                mEtKeyword.setText("");
                mListener.onSearch(mEtKeyword.getText().toString().trim());
                break;
            case R.id.llSearchBtn:
                showHideEdit();
                break;
            default:
                break;
        }
    }

    private void showHideEdit(){
        if(mIsShowEdit){
            mIsShowEdit = false;
            mLlEditText.setVisibility(GONE);
            mLlSearchBtn.setVisibility(VISIBLE);
        }else{
            mIsShowEdit = true;
            mLlEditText.setVisibility(VISIBLE);
            mLlSearchBtn.setVisibility(GONE);
        }
        showHideKeyboard();
    }

    public void showHideKeyboard(){
        if(imm == null){
            return;
        }
        if(mIsShowEdit){
            mEtKeyword.requestFocus();
            imm.showSoftInput(mEtKeyword, 0);
        }else{
            imm.hideSoftInputFromWindow(mEtKeyword.getWindowToken(), 0);
        }
    }
}
